#include"MessageDialog.h"
MessageDialog::MessageDialog()
{}
MessageDialog::MessageDialog(char *title, int w, int h, char *filename) : Window(title, w, h)
{
	Filename = new char[STR];
	Offset = new long[MAX];
	strcpy(Filename, filename);
	CurrentIndex = 0;
	MaxIndex = -1;
	Offset[CurrentIndex] = 0L;
}
MessageDialog::~MessageDialog()
{
	delete Filename;
	delete Offset;
}
int MessageDialog::createMessageDialog(char *title, int w, int h, char *filename)
{
	createWindow(title, w, h);
	Filename = new char[STR];
	Offset = new long[MAX];
	strcpy(Filename, filename);
	CurrentIndex = 0;
	MaxIndex = -1;
	Offset[CurrentIndex] = 0L;
	return fileExists(Filename);
}
void MessageDialog::setXY(int x, int y)
{
	Window::setXY(x, y);
}
inline int MessageDialog::isReady()
{
	return fileExists(Filename);
}
inline Rectangle MessageDialog::getMessageDialogRect()
{
	return getWindowRect();
}
void MessageDialog::showMessageDialog()
{
	showWindow();
	showMessage();
}
void MessageDialog::showMessage()
{
	int x, y, left, width, height, BottomFlag, DispFlag;
	char ch;
	FILE *srcFile;
	x = getWindowRect().left + 2;
	y = getWindowRect().top + 2;
	width = x + getWidth() - 3; // Get Window Width to calculate internal
	height = getHeight() + 1; // window where the message is displayed
	left = x; // Store the left position for future use

	srcFile = fopen(Filename, "r");
	if ( srcFile ) // If valid file
	{
		if ( CurrentIndex >= 0 && CurrentIndex < MAX ) // If valid CurrentIndex
		{
			fseek(srcFile, Offset[CurrentIndex], SEEK_SET); // Goto current position
			BottomFlag = 0;
			ch = fgetc(srcFile);
			while ( !BottomFlag && ch != EOF )
			{
				DispFlag = 1; // Set flag for display
				if ( ch == '\t' )
				{
					x += 7; // Move 7 spaces
					DispFlag = 0; // If tab reset display flag
				}
				if ( x >= width) // If exceeds width
				{
					x = left;
					y++; // Goto next line
				}
				if ( ch == '\n' )
				{
					x = left; // Reset x position to the left
					y++; // Goto next line
					DispFlag = 0; // If newline don't display
				}
				if ( y >= height ) // If the vertical limit has been reached
				{
					BottomFlag = 1; // Loop terminating condition
					DispFlag = 0; // Don't display (ofcourse)
				}

				if ( DispFlag ) // Put the character
					putChar(x++, y, ch, MSG_TXT);
				ch = fgetc(srcFile); // Read next character
			} // Endwhile (EOF, BottomFlag)
			if ( BottomFlag )
				Offset[CurrentIndex + 1] = ftell(srcFile) - 2; // Store new offset
			if ( ch == EOF )
				MaxIndex = CurrentIndex;
			fclose(srcFile);
		} // Endif (valid CurrentIndex)
	} // Endif (Source file opened)
}
void MessageDialog::handleMessageDialog()
{
	int ch;
	while ( 1 )
	{
		if ( kbhit() )
		{
			ch = getKey();
			switch ( ch )
			{
				case   UP: // Up
									 CurrentIndex -= (CurrentIndex <= 0) ? 0 : 1;
									 showMessageDialog();
									 break;
				case DOWN: // Down
									 if ( MaxIndex == -1 )
										 CurrentIndex += (CurrentIndex < (MAX - 1)) ? 1 : 0;
									 else
										 CurrentIndex += (CurrentIndex < MaxIndex) ? 1 : 0;
									 showMessageDialog();
									 break;
				case  ESC: return;
			} // Endswitch
		} // Endif (kbhit)
	} // Endwhile (Infinite)
}
// Class MessageDialogEnds